package com.android.ailaoshan.collection.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.ailaoshan.R;
import com.android.ailaoshan.collection.CustomFileInfo;
import com.android.ailaoshan.collection.ManagerFile;
import com.android.ailaoshan.collection.PictureAdapter;
import com.android.ailaoshan.picture.PictureActivity;
import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class PictureFragment extends Fragment implements OnItemClickListener{

    private Context mContext;

    private View pictureView;
    private QMUILoadingView loadingView;
    private SwipeRecyclerView recyclerView;

    private ArrayList<CustomFileInfo> resourceInfoList = new ArrayList<>();
    private PictureAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();

        pictureView = inflater.inflate(R.layout.picture_fragment,container,false);
        return pictureView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        recyclerView = pictureView.findViewById(R.id.collection_content_picture_recyclerView);

        // 布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);

        // 分隔线
        RecyclerView.ItemDecoration itemDecoration =
                new DefaultItemDecoration(ContextCompat.getColor(mContext, R.color.tab_panel_divider));
        recyclerView.addItemDecoration(itemDecoration);

        // 点击监听
        recyclerView.setOnItemClickListener(this);

        // 添加右侧菜单
        recyclerView.setSwipeMenuCreator(swipeMenuCreator);
        recyclerView.setOnItemMenuClickListener(mMenuItemClickListener);

        // 适配器
        adapter = new PictureAdapter(mContext);
        recyclerView.setAdapter(adapter);

        // 从数据库中加载数据
        MyHandler myHandler = new MyHandler();
        ManagerFile managerFile = new ManagerFile();
        managerFile.searchAllFile("1", myHandler);
    }

    @Override
    public void onStart(){
        super.onStart();

        // 显示正在加载
        loadingView = pictureView.findViewById(R.id.collection_picture_loading);
        loadingView.setVisibility(View.VISIBLE);
        loadingView.setActivated(true);
        loadingView.start();
    }

    /**
     * 创建右侧菜单
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
            int width = getResources().getDimensionPixelOffset(R.dimen.collection_dp_70);
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            {
                SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
                        .setBackground(R.drawable.selector_red)
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                rightMenu.addMenuItem(deleteItem);
            }
        }
    };

    /**
     * 右侧菜单点击事件
     */
    private OnItemMenuClickListener mMenuItemClickListener = new OnItemMenuClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge, int position) {
            menuBridge.closeMenu();

            int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。

            if (direction == SwipeRecyclerView.RIGHT_DIRECTION) {
                if(resourceInfoList != null){
                    ArrayList<String> deleteFileName = resourceInfoList.get(position)
                            .getMoreFileName();
                    long fileNo = resourceInfoList.get(position).getFileNo();

                    ManagerFile managerFile = new ManagerFile();
                    managerFile.deleteFile(fileNo,deleteFileName,1);

                    resourceInfoList.remove(position);
                    if(adapter != null){
                        adapter.notifyDataSetChanged(resourceInfoList);
                    }
                }
            }
        }
    };

    /**
     * 从数据库加载数据后回调
     */
    private class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {

            // 停止显示正在加载
            if(loadingView != null){
                loadingView.setActivated(false);
                loadingView.setVisibility(View.INVISIBLE);
                loadingView.stop();
            }

            if(msg.what == 605){
                if(resourceInfoList != null){
                    resourceInfoList = (ArrayList<CustomFileInfo>) msg.obj;
                    if(adapter != null){
                        adapter.notifyDataSetChanged(resourceInfoList);
                    }
                }
            }

            // 显示视图
            if(recyclerView != null){
                recyclerView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 每一条被点击时
     */
    @Override
    public void onItemClick(View view, int position) {
        if(resourceInfoList != null){
            CustomFileInfo clickItem = resourceInfoList.get(position);
            ArrayList<String> showPage = clickItem.getMoreFileName();

            if(showPage.size() > 0){
                Intent intent = new Intent(mContext, PictureActivity.class);
                if(clickItem.isUpload()){
                    intent.putExtra("saveType",3);
                }else{
                    intent.putExtra("saveType",2);
                }
                intent.putStringArrayListExtra("picName",showPage);
                intent.putExtra("describe",clickItem.getDescribe());

                startActivityForResult(intent,609);
            }
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();

        mContext = null;
        pictureView = null;
        loadingView = null;
        recyclerView = null;

        resourceInfoList = null;
        adapter = null;
    }
}